package com.liuda.settings.controller;

import com.liuda.commons.constants.Constants;
import com.liuda.commons.domain.ReturnObject;
import com.liuda.commons.utils.DateUtil;
import com.liuda.settings.domain.User;
import com.liuda.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/settings/user/toLogin.try")
    public String toLogin(){
        return "settings/user/login";
    }
    @RequestMapping("/settings/user/login.try")
    @ResponseBody
    public Object Login(HttpServletRequest request, HttpServletResponse response,String loginAct, String loginPwd, String isPwdFree){
        ReturnObject object=new ReturnObject();
        if(loginAct.equals("")){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("请输入账号！");
            return object;
        }
        if(loginPwd.equals("")){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("请输入密码！");
            return object;
        }
        User user= userService.loginVerify(loginAct,loginPwd);
        if(user==null){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("请检查账号或密码是否正确！");
        }else{
            if(!user.getAllowIps().contains(request.getRemoteAddr())){
                object.setState(Constants.RETURN_OBJECT_FAILURE);
                object.setMsg("IP受限！");
            }else{
                if(user.getLockState().equals(Constants.ACT_LOCKED)){
                    object.setState(Constants.RETURN_OBJECT_FAILURE);
                    object.setMsg("该账号已被冻结！");
                }else{
                    if(DateUtil.formatDateTime(new Date()).compareTo(user.getExpireTime())>0){
                        object.setState(Constants.RETURN_OBJECT_FAILURE);
                        object.setMsg("该账号已过期！");
                    }else{
                        object.setState(Constants.RETURN_OBJECT_SUCCESS);
                        HttpSession session = request.getSession();
                        session.setAttribute(Constants.SESSION_USER,user);
                        Cookie actCookie=new Cookie("loginAct",loginAct);
                        Cookie pwdCookie=new Cookie("loginPwd",loginPwd);
                        if(isPwdFree.equals("true")){
                            actCookie.setMaxAge(10*24*60*60);
                            pwdCookie.setMaxAge(10*24*60*60);
                            response.addCookie(actCookie);
                            response.addCookie(pwdCookie);
                        }else{
                            Cookie[] cookies = request.getCookies();
                            Cookie reActCookie=null;
                            Cookie rePwdCookie=null;
                            for (Cookie cookie:cookies) {
                                if(cookie.getName().equals("loginAct")){
                                    reActCookie=cookie;
                                }
                                if(cookie.getName().equals("loginPwd")){
                                    rePwdCookie=cookie;
                                }
                            }
                            if(reActCookie!=null && rePwdCookie!=null && (!reActCookie.getValue().equals(loginAct) || !rePwdCookie.getValue().equals(loginPwd)))
                            {
                                actCookie.setMaxAge(0);
                                pwdCookie.setMaxAge(0);
                                response.addCookie(actCookie);
                                response.addCookie(pwdCookie);
                            }
                        }
                    }
                }
            }
        }
        return object;
    }
    @RequestMapping("settings/user/logout.do")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession(false);
        if(session!=null)
            session.invalidate();
        Cookie actCookie=new Cookie("loginAct","");
        Cookie pwdCookie=new Cookie("loginPwd","");
        actCookie.setMaxAge(0);
        pwdCookie.setMaxAge(0);
        response.addCookie(actCookie);
        response.addCookie(pwdCookie);
        return "redirect:toLogin.try";
    }
}
